package site.xuxingcan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;

public class MysqlUtil {
    private static volatile MysqlUtil instance;
    public static String MYSQL_URI = "45.32.226.3:3306/phone_call", MYSQL_USER = "phoneCall", MYSQL_PWD = "phone_call";
    private static String MYSQL_URL = "jdbc:mysql://" + MYSQL_URI + "?user=" + MYSQL_USER + "&password=" + MYSQL_PWD + "&useUnicode=true&characterEncoding=UTF8";
    private Connection conn;

    public static MysqlUtil getInstance() {
        if (instance == null) {
            synchronized (MysqlUtil.class) {
                if (instance == null) {
                    instance = new MysqlUtil();
                }
            }
        }
        return instance;
    }

    private MysqlUtil() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(MYSQL_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(10 * 60 * 1000);
                        heartBeat();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public boolean isKeyRight(String key) {
        if (key == null || key.length() == 0) {
            return false;
        }
        try {
            String sql = "select auth_key from auth_table;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            String auth_key = "";
            while (rs.next()) {// 判断是否还有下一个数据
                auth_key = rs.getString("auth_key");
            }
            rs.close();
            ps.close();
            return key.equalsIgnoreCase(auth_key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean isIMEIKeyRight(String key) {
        if (key == null || key.length() == 0) {
            return false;
        }
        try {
            String sql = "select auth_key from imei_auth_table;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            String auth_key = "";
            while (rs.next()) {// 判断是否还有下一个数据
                auth_key = rs.getString("auth_key");
            }
            rs.close();
            ps.close();
            return key.equalsIgnoreCase(auth_key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public String getDianxinContent() {
        try {
            String sql = "select content from imei_dianxin;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            String content = "";
            while (rs.next()) {// 判断是否还有下一个数据
                content = rs.getString("content");
            }
            rs.close();
            ps.close();
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public String getLiantongContent() {
        try {
            String sql = "select content from imei_liantong;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            String content = "";
            while (rs.next()) {// 判断是否还有下一个数据
                content = rs.getString("content");
            }
            rs.close();
            ps.close();
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public ArrayList<String> getAuthUrlList() {
        ArrayList<String> list = new ArrayList<>();
        try {
            String sql = "select url from auth_url;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                String user_id = rs.getString("url");
                if (user_id != null && user_id.length() > 0) {
                    list.add(user_id);
                }
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public ArrayList<String> getNormalUrlList() {
        ArrayList<String> list = new ArrayList<>();
        try {
            String sql = "select url from normal_url;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                String user_id = rs.getString("url");
                if (user_id != null && user_id.length() > 0) {
                    list.add(user_id);
                }
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public void heartBeat() {
        try {
            System.out.println("heartBeat  " + isKeyRight("666") + "    at " + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
